<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */
?>
<div class="g_a_core_linktoolbox">
	<div style="height: 300px; overflow: auto;">
		<?php foreach ($this->links as $route => $links) : ?>
		<h3><?php echo $links['description']; ?></h3>
		<div class="g_a_bottom">
			<ul>
			<?php foreach ($links['links'] as $link) : ?>
				<li>
					<a href="<?php echo $link['href']; ?>" 
					   class="g_a_ui_helper_action g_a_core_linktoolbox_draggable" 
					   route="<?php echo $route; ?>" 
					   title="<?php if (isset($link['title'])) : ?><?php echo $link['title']; ?><?php elseif (isset($link['text'])) : ?><?php echo $link['text']; ?><?php endif; ?>"><?php if (isset($link['text'])) : ?><?php echo $link['text']; ?><?php else : ?><?php echo $links['description']; ?><?php endif; ?></a>
					<a class="fr" href="javascript: void(0);" style="padding-right: 5px;"><?php echo $this->TRANSLATOR->widget('select'); ?></a>
				</li>
			<?php endforeach; ?>
			</ul>
		</div>
		<?php endforeach; ?>
	</div>	
</div>

<script type="text/javascript">
'Gio.Core.Widgets.LinkToolbox'.namespace();

Gio.Core.Widgets.LinkToolbox.handleDrop = function(droppedElement, event, ui) {
	if (!$(ui.helper).hasClass('g_a_core_linktoolbox_draggable')) {
		return;
	}
	var data = $(ui.helper).attr('widget_data');
	data     = $.evalJSON(data);

	switch (true) {
		/** Textbox, textarea */
		case ($(droppedElement).attr('type') == 'text' || $(droppedElement).attr('type') == 'textarea'):
			$(droppedElement).data('core_linktoobox', data)
							 .attr('core_linktoobox', $.toJSON(data))
							 .val($(droppedElement).val() + data.href);
			break;
			
		/** WYSIWYG */
		case ($(droppedElement).find('textarea.g_a_ui_editor').length > 0):
			/** Gets the Id of editor */
			var id      = $(droppedElement).find('textarea.g_a_ui_editor:first').attr('id');
			var factory = new Gio.Core.Editor.Factory();
			factory.factory('<?php //echo $this->editor; ?>')
				   .setId(id)
				   .insertHtml('<a href="' + data.href + '" title="' + data.title + '">' + data.title + '</a>');
			break;
	}
};

$(document).ready(function() {
	/**
	 * Create accordion control
	 */
	$('.g_a_core_linktoolbox div:first').accordion({
		active: true,
		autoHeight: false,
		collapsible: true,
		icons: {
			header: 'maximum',
			headerSelected: 'minimum'
		}
	});
	
//	$('a.g_a_core_linktoolbox_draggable').each(function() {
//		var data = $.toJSON({
//			href: $(this).attr('href'),
//			route: $(this).attr('route'),
//			title: ($(this).attr('title') == '') ? $(this).html() : $(this).attr('title'),
//			text: $(this).html()
//		});
//		$(this).attr('widget_data', data).draggable({
//			helper: 'clone',
//			revert: 'invalid'	
//		}).disableSelection();
//	});
	
	/**
	 * Make the textbox, textarea, WYSIWYG editors droppable
	 */
	$([
		$('input[type="text"], textarea'), 
		$('.g_a_ui_editor').parent()
	]).each(function() {
		$(this).droppable({
			drop: function(event, ui) {
				Gio.Core.ToolBox.callDropHandler($(this), 'Gio.Core.Widgets.LinkToolbox.handleDrop', event, ui);
			}
		});
	});
});
</script>